package com.syezon.wifikey.news.data;

import com.syezon.wifikey.news.config.NewsConfig;
import com.syezon.wifikey.news.info.NewsInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/10/15.
 */

public class NewsData {

    private static String rowkey = "";

    /**
     * @param isFirst  是否是第一次加载数据
     * @param isShowAd 是否关闭推广广告
     * @return 新闻列表集合
     * @throws JSONException
     */
    public static List<NewsInfo> getNewsRecommendList(boolean isFirst, boolean isShowAd, String newsSource, int pageNum) throws JSONException, IOException {
        List<NewsInfo> newsInfos = new ArrayList<>();
        if (newsSource.equals(NewsConfig.NEWS_SOURCE_DF)) {
            newsInfos.addAll(getNewsFromDF(isFirst));
        } else if (newsSource.equals(NewsConfig.NEWS_SOURCE_TT)) {
            newsInfos.addAll(getNewsFromTT(pageNum));
        } else {
            newsInfos.addAll(getNewsFromSelf(isFirst, isShowAd));
        }
        return newsInfos;
    }

    private static List<NewsInfo> getNewsFromDF(boolean isFirst) throws IOException, JSONException {
        List<NewsInfo> newsInfos = new ArrayList<>();
        String response = "";
        if (isFirst || rowkey.equals("")) {
            response = HttpUtils.getResponse(NewsConfig.URL_EAST_NEWS);
        } else {
            response = HttpUtils.getResponse("https://newswifiapi.dftoutiao.com/jsonnew/next?type=toutiao&startkey="
                    + rowkey + "&qid=wifixhwy&ispc=0&num=20");
        }

        JSONObject json = new JSONObject(response);
        JSONArray jsonArray = json.optJSONArray("data");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject bean = jsonArray.optJSONObject(i);
            NewsInfo info = new NewsInfo();
            info.source = bean.optString("source");
            info.author = bean.optString("source");
            info.advUrl = bean.optString("url");
            info.date = bean.optString("date");
            info.pk = bean.optString("rowkey");
            info.name = bean.optString("topic");
            info.type = 1;
            info.clickUrl = bean.optString("url");

            JSONArray imgs = bean.optJSONArray("miniimg02");
            List<String> imgPaths = new ArrayList<>();
            StringBuffer sb = new StringBuffer();
            int size = 0;
            for (int j = 0; j < imgs.length(); j++) {
                JSONObject temp = imgs.optJSONObject(j);
                String img1 = temp.optString("src");
                if (j == 0) info.picUrl = img1;
                sb.append(img1);
                if (j != imgs.length() - 1) {
                    sb.append("|");
                }
                size++;
            }
            info.imgs = sb.toString();
            if (size >= 3) {
                info.infoType = (NewsInfo.NEWS_TYPE_THREE_PIC);
            } else {
                info.infoType = (NewsInfo.NEWS_TYPE_SINGLE_PIC);
            }
            newsInfos.add(info);
            if (i == jsonArray.length() - 1) rowkey = bean.optString("rowkey");
        }
        return newsInfos;
    }

    private static List<NewsInfo> getNewsFromTT(int num) throws IOException, JSONException {
        List<NewsInfo> newsInfos = new ArrayList<>();
        String result = "";
        result = HttpUtils.requestCompanionNews(HttpUtils.getTTNews + num);
        JSONObject json = new JSONObject(result);
        JSONArray objects = json.optJSONArray("data");
        for (int i = 0; i < objects.length(); i++) {
            JSONObject obj = objects.optJSONObject(i);
            NewsInfo newsInfo = new NewsInfo();
            newsInfo.author = obj.optString("author");
            newsInfo.name = obj.optString("title");
            newsInfo.source = obj.optString("author");
            String picUrl = obj.optString("pic1");
            if (picUrl.equals("")) {
                newsInfo.infoType = NewsInfo.NEWS_TYPE_SIMPLE_TEXT;
            } else {
                newsInfo.infoType = NewsInfo.NEWS_TYPE_SINGLE_PIC;
                newsInfo.picUrl = picUrl;
            }
            newsInfo.type = 1;
            newsInfo.clickUrl = obj.optString("url");
            newsInfo.date = obj.optString("newsTime");
            newsInfo.id = 888;
            newsInfos.add(newsInfo);
        }

        return newsInfos;
    }

    /**
     * 从自有服务器获取新闻
     *
     * @param isFirst
     * @param isShowAd
     * @return
     * @throws IOException
     * @throws JSONException
     */
    private static List<NewsInfo> getNewsFromSelf(boolean isFirst, boolean isShowAd) throws IOException, JSONException {
        List<NewsInfo> newsInfos = new ArrayList<>();
        String result = "";
        if (isFirst) {
            result = HttpUtils.requestNewsRecommend();
        } else {
            result = HttpUtils.requestDynamicNewsRecommend();
        }
        if (HttpUtils.checkResult(result)) {
            JSONObject json = new JSONObject(result).optJSONObject("data");
            if (json != null) {
                JSONArray newsArray = json.optJSONArray("recommend");
                if (newsArray != null && newsArray.length() > 0) {
                    for (int i = 0; i < newsArray.length(); i++) {
                        JSONObject newsObject = newsArray.optJSONObject(i);
                        NewsInfo newsInfo = new NewsInfo();
                        newsInfo.id = newsObject.optInt("id");
                        newsInfo.author = newsObject.optString("author");
                        newsInfo.name = newsObject.optString("title");
                        newsInfo.source = newsObject.optString("source");
                        newsInfo.type = newsObject.optInt("type");
                        newsInfo.clickUrl = newsObject.optString("url");
                        newsInfo.date = newsObject.optString("date");
                        newsInfo.infoType = newsObject.optInt("infoType");
                        newsInfo.picUrl = newsObject.optString("picture");
                        newsInfo.position = newsObject.optLong("position");
                        JSONArray imgs = newsObject.optJSONArray("imgs");
                        if ((newsInfo.infoType == 3 || newsInfo.infoType == 2) && imgs != null && imgs.length() > 0) {
                            StringBuffer sb = new StringBuffer();
                            for (int k = 0; k < imgs.length(); k++) {
                                JSONObject imgObject = imgs.optJSONObject(k);
                                String img1 = imgObject.optString("url");
                                sb.append(img1);
                                if (k != imgs.length() - 1) {
                                    sb.append("|");
                                }
                            }
                            newsInfo.imgs = sb.toString();

                        }
                        newsInfo.recommend = 1;
                        if (!isShowAd) {//关闭资讯的广告
                            if (newsInfo.type == 0) {
                                continue;
                            }
                        }

                        if (newsInfo.isDataValid(newsInfo)) {//过滤掉不完整数
                            newsInfos.add(newsInfo);
                        }
                    }
                }
            }
        }
        return newsInfos;
    }

}
